package com.sh.rbac.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sh.rbac.annotation.OperationLogger;
import com.sh.rbac.domain.dto.dict.DictQueryDTO;
import com.sh.rbac.domain.dto.dict.DictSaveDTO;
import com.sh.rbac.domain.dto.dict.DictUpdateDTO;
import com.sh.rbac.domain.entity.Dict;
import com.sh.rbac.domain.enums.OperationTypeEnum;
import com.sh.rbac.result.Result;
import com.sh.rbac.service.DictService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 字典控制器
 *
 * @author lucky
 * @since 2024-06-26
 */

@Tag(name = "字典管理", description = "字典相关操作")
@RequestMapping("sys/dict")
@RestController
public class DictController {

    @Autowired
    private DictService dictService;

    @Operation(summary = "查询字典列表")
    @ApiResponse(responseCode = "200", description = "返回查询结果", content = @Content(schema = @Schema(implementation = Dict.class)))
    @GetMapping("list")
    @PreAuthorize("hasAuthority('sys:dict:list')")
    public Result<List<Dict>> getList() {
        return Result.success(dictService.list());
    }


    @Operation(summary = "分页查询字典列表", description = "根据字典条件查询参数进行查询")
    @ApiResponse(responseCode = "200", description = "返回查询结果", content = @Content(schema = @Schema(implementation = Dict.class)))
    @PostMapping("page/{current}/{limit}")
    @PreAuthorize("hasAuthority('sys:dict:page')")
    public Result<Page<Dict>> getPage(@PathVariable Long current, @PathVariable Long limit, @RequestBody DictQueryDTO dictQueryDTO) {
        return Result.success(dictService.getPage(current, limit, dictQueryDTO));
    }


    @OperationLogger(module = "系统管理->字典管理", type = OperationTypeEnum.ADD)
    @Operation(summary = "添加字典", description = "保存一个新字典")
    @ApiResponse(responseCode = "200", description = "成功保存字典")
    @PostMapping("save")
    @PreAuthorize("hasAuthority('sys:dict:save')")
    public Result<Boolean> save(@RequestBody DictSaveDTO dictSaveDTO) {
        return dictService.save(dictSaveDTO) ? Result.success() : Result.failure();
    }


    @OperationLogger(module = "系统管理->字典管理", type = OperationTypeEnum.UPDATE)
    @Operation(summary = "更新字典", description = "根据ID更新字典信息")
    @ApiResponse(responseCode = "200", description = "成功更新字典")
    @PutMapping("update")
    @PreAuthorize("hasAuthority('sys:dict:update')")
    public Result<Boolean> update(@RequestBody DictUpdateDTO dictUpdateDTO) {
        return dictService.updateById(dictUpdateDTO) ? Result.success() : Result.failure();
    }

    @OperationLogger(module = "系统管理->字典管理", type = OperationTypeEnum.DELETE)
    @Operation(summary = "删除字典", description = "根据ID删除字典信息")
    @ApiResponse(responseCode = "200", description = "成功删除字典")
    @DeleteMapping("delete/{id}")
    @PreAuthorize("hasAuthority('sys:dict:delete')")
    public Result<Boolean> delete(@PathVariable Long id) {
        return dictService.removeById(id) ? Result.success() : Result.failure();
    }

    @Operation(summary = "根据ID查询字典")
    @ApiResponse(responseCode = "200", description = "返回查询结果", content = @Content(schema = @Schema(implementation = Dict.class)))
    @GetMapping("get/{id}")
    @PreAuthorize("hasAuthority('sys:dict:get')")
    public Result<Dict> get(@PathVariable Long id) {
        return Result.success(dictService.getById(id));
    }


    @OperationLogger(module = "系统管理->字典管理", type = OperationTypeEnum.UPDATE)
    @Operation(summary = "更新字典状态", description = "根据ID更新字典状态")
    @ApiResponse(responseCode = "200", description = "成功更新字典状态")
    @PutMapping("update-status/{id}/{status}")
    @PreAuthorize("hasAuthority('sys:dict:update-status')")
    public Result<Boolean> updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        return dictService.updateStatus(id, status) ? Result.success() : Result.failure();
    }
}
